package com.romadan.core.wolfram;

import com.wolfram.jlink.KernelLink;
import com.wolfram.jlink.MathLinkException;
import com.wolfram.jlink.MathLinkFactory;

public class FactoryKernelLink {
	public FactoryKernelLink() {
		KernelDir kd = new KernelDir();
		fileWolframKernel = kd.getKernelFile();
	}

	private String fileWolframKernel = "";
	private KernelLink myKernel = null;
	public static Object monitor = new Object();

	private KernelLink createKernel() throws MathLinkException {
		KernelLink ml = null;
		String[] mlArgs = { "-linkmode", "launch", "-linkname",
				fileWolframKernel };
		ml = MathLinkFactory.createKernelLink(mlArgs);
		ml.discardAnswer();
		return ml;
	}

	public KernelLink getKernel() {
		KernelLink result = null;
		if (myKernel == null) {
			try {
				myKernel = createKernel();
			} catch (MathLinkException e) {
				e.printStackTrace();
			}
			result = myKernel;
		} else {
			result = myKernel;
		}
		return result;
	}
}
